package userInterface;

import javax.swing.JPanel;
import java.awt.GridBagLayout;
import javax.swing.JTextField;
import java.awt.GridBagConstraints;
import javax.swing.JButton;
import java.awt.Insets;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JEditorPane;
import javax.swing.JOptionPane;
import javax.swing.JSeparator;

import controller.Controller;
import domain.Movie;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.SQLException;
import org.jdesktop.beansbinding.BeanProperty;
import org.jdesktop.beansbinding.AutoBinding;
import org.jdesktop.beansbinding.Bindings;
import org.jdesktop.beansbinding.AutoBinding.UpdateStrategy;

public class SearchandRankPanel extends JPanel {
	private Controller controller;
	private JTextField textField;
	private JTextField textField_1;
	private Movie moviesearched;
	private JEditorPane editorPane;
	private JLabel lblNewLabel;
	

	/**
	 * Create the panel.
	 */
	public SearchandRankPanel(final Controller controller) {
		this.controller = controller;
		setLayout(null);
		JPanel panel = new JPanel();
		panel.setBounds(23, 11, 659, 105);
		add(panel);
		GridBagLayout gbl_panel = new GridBagLayout();
		gbl_panel.columnWidths = new int[]{0, 0};
		gbl_panel.rowHeights = new int[]{0, 0, 0, 0};
		gbl_panel.columnWeights = new double[]{1.0, Double.MIN_VALUE};
		gbl_panel.rowWeights = new double[]{0.0, 0.0, 0.0, Double.MIN_VALUE};
		panel.setLayout(gbl_panel);
		
		textField = new JTextField();
		GridBagConstraints gbc_textField = new GridBagConstraints();
		gbc_textField.insets = new Insets(0, 0, 5, 0);
		gbc_textField.fill = GridBagConstraints.HORIZONTAL;
		gbc_textField.gridx = 0;
		gbc_textField.gridy = 1;
		panel.add(textField, gbc_textField);
		textField.setColumns(10);
		
		JButton searchButton = new JButton("Search");
		searchButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				try {
					moviesearched = controller.searchMovie(textField.getText());
					if(moviesearched == null) {
						JOptionPane.showMessageDialog(new JFrame(), "No movie exist with a name "+"\""+textField.getText()+ "\"", "Error", JOptionPane.ERROR_MESSAGE);
					}
					else {
						RateDialog rDialog = new RateDialog(moviesearched, controller);
						rDialog.setVisible(true);
					}
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
			}
		});
		GridBagConstraints gbc_searchButton = new GridBagConstraints();
		gbc_searchButton.gridx = 0;
		gbc_searchButton.gridy = 2;
		panel.add(searchButton, gbc_searchButton);
		
		JSeparator separator = new JSeparator();
		separator.setBounds(10, 127, 695, 2);
		add(separator);
		initDataBindings();
		

		
		

	}
	protected void initDataBindings() {
	}
}
